package com.dosh;

import android.app.Activity;
import android.database.Cursor;
/**
 * The Item class describes an entry in the Item table.
 * A User can use Items an relate them with a Booking.  
 * The fields item_ID, name and category are necessary for are table entry.
 */
public class Item extends Activity{
	/**
	 * The ID of the Item entry
	 */
	public long item_ID;
	/**
	 * The Items name
	 */
	public String name;
	/**
	 * The Items Category
	 */
	public Category category;
	
	/**
	 * This Constructor is used, if there is no entry in the database.
	 * @param dba A reference to the DBAdapter
	 */
	public Item(String name, Category category, DBAdapter dba) {

		this.name = name;
		this.category = category;
		
		dba.open();
		this.item_ID = dba.insertItem(name, category.category_ID);
		dba.close();
	}
	/**
	 * This Constructor is used, if there an entry already exists in the database.
	 * @param dba A reference to the DBAdapter
	 */
	public Item(long item_ID, DBAdapter dba) {

		this.item_ID = item_ID;
		dba.open();
		Cursor iCursor = dba.SELECTbyId("Item", item_ID);
		dba.close();
		this.name = iCursor.getString(1);
		this.category = new Category(iCursor.getLong(2), dba);

	}
	/**
	 * This method updates the table entry
	 * @param dba A reference to the DBAdapter
	 * @return gives information,if the update was successful
	 */
	public boolean update(DBAdapter dba) 
	{
		boolean bool = true;
		
		dba.open();
		bool = dba.update("Item", this.item_ID, "Name", this.name);
		bool = dba.update("Item", this.item_ID, "Category", this.category.category_ID);
		dba.close();
		
		return bool;
	}

}
